<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/component/commonTagLib.jsp"%>
<!DOCTYPE html>
<html>
<head>
<title><spring:message code="message.lable.GouXuanMuLu"/></title>
<%@ include file="/WEB-INF/component/commonCSS.jsp"%>
<!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue sidebar-mini fixed skin-blue-light-frame">
	<section class="content-header">
		<h1>采购目录维护</h1>
		<ol class="breadcrumb">
			<li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i><spring:message code="message.lable.first"/></a></li>
			<li><a href="#">交易</a></li>
			<li class="active"><a href="#">采购目录维护</a></li>
		</ol>
	</section>

	<section class="content">
		<div class=" box box-success">
			<div class="box-body">
				<div class="input">
					
                    <div class="textalign1"><spring:message code="message.listForm.tongYongMing"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="qualityspecName" name="qualityspecName" placeholder="<spring:message code="message.listForm.tongYongMing"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
                    <%--<div class="textalign"><spring:message code="message.listForm.yaoPinBianMa"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="drugCode" name="drugCode"  placeholder="<spring:message code="message.listForm.yaoPinBianMa"/>" />
                    </div>--%>
                    <div class="textalign1"><spring:message code="message.listForm.shengChanQiYe"/><spring:message code="message.yanZheng.maoHao"/></div>
                    <div class="boxinput">
                        <input type="text" class="textinput" id="prodCompname" name="prodCompname" placeholder="<spring:message code="message.listForm.shengChanQiYe"/>/<spring:message code="message.listForm.pinYinMa"/>" />
                    </div>
					<div class="textalign1" >
						<button type="button" onclick="gridSerch();" class="btn btn-success btn-sm" id="selectDicCompType"><spring:message code="message.button.seachSpacing"/></button>
					</div>
                 </div>
			</div>
		</div>


		<table class="jqgrid" id="gridlist"></table>
		<div id="gridpage"></div>
		<div class=" box box-warning bottongroup">

			<button id="jrml" type="button" onclick="batchSaveGoodsSelect()" class="btn btn-primary btn-sm"><spring:message code="message.button.JiaRuMuLu"/></button>
			<label id="lxtj" onclick="window.location.href='${ctx}/purGoodsSelect/toPurGoodsSelect.html';" class="btn btn-danger btn-sm">返 回</label>
		</div>
	</section>
	<%@ include file="/WEB-INF/component/commonJS.jsp"%>
	<script type="text/javascript">
	//绑定回车
    $('#projName,#drugCode,#qualityspecName,#prodCompname,#isLongtimePlan,#goodsSource').bind('keydown', function(event) {
        if (event.keyCode == "13") {
            $('#selectDicCompType').click();
        }
    });	
    $("#qualityspecName").focus();//加载页面光标在第一个查询框内
    
        $(function () {
        	$("#projId").HNSelect({
                url: "${pageContext.request.contextPath}/selectController/getMyProj.html", data: { ID: '' },  defaultText: "<option value=''><spring:message code="message.select.option"/></option>",
            });
        	$("#gridlist").jqGrid({
                url: "${ctx}/purHospCatalog/getGoodsSelectList.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                shrinkToFit: false,
              //  height: 230,
                colNames: [
                           'prodCompcode',
                           'projID', 
						   '<spring:message code="message.jqGrid.cz"/>',
						   '<spring:message code="message.model.peiSongQiYe"/>',
						   '中标价',
                           '中标后零售价',
                           '<spring:message code="message.listForm.CaiGouJia"/>',
                           '<spring:message code="message.listForm.yaoPinBianMa"/>',
                           '<spring:message code="message.listForm.tongYongMing"/>', 
                           '<spring:message code="message.listForm.jiXing"/>',
                           '<spring:message code="message.listForm.guiGe"/>',
                           '<spring:message code="message.listForm.zhuanHuanXiShu"/>',
                           '<spring:message code="message.listForm.baoZhuangDanWei"/>', 
                           '<spring:message code="message.listForm.caiZhi"/>',
                           '<spring:message code="message.listForm.shengChanQiYe"/>',
                           '药品类别',
                           '<spring:message code="message.listForm.shangBianMa"/>',
                           '项目名称',
                           '药品来源','','<spring:message code="message.listForm.ZhongBiaoQiYe"/>',
						   ''
                           ],
                colModel: [
					
					{ name:'prodCompcode',index:'n1', width:60, hidden:true,align:"center", sortable:false  },
					{ name:'projId',index:'n1', width:60, hidden:true,align:"center", sortable:false  },//项目ID
					{ name: 'oper', index: 'ship',width:60, align: "center" , sortable:false},
					{ name: 'delCompName', index: 'delCompName', width: 200, align: "center", sortable:false},
					{ name: 'proPriceLimit', index: 'ship', width: 80, align: "right", sortable:false,
						 formatter:function(val, options, row){
		                    	return formatAmount(val, options, row);
		                    }			
					},
					{ name: 'retailPriceLimit', index: 'ship', width: 80, align: "right", sortable:false},
                    { name: 'proPrice', index: 'ship', hidden:true,width: 80, align: "right", sortable:false},
					{ name:'drugCode',index:'drugCode', hidden:true,width:100,align:"center" , sortable:false,
						formatter:function(cellvalue, options, row) {
							return "<a href=\"javascript:void(0);\" onclick=\"showDrugInfo('"+cellvalue+"', '"+row.qualityspecName+"')\">"+cellvalue+"</a>"
                    	}   },
                    { name: 'qualityspecName', index: 'name', width: 140,align:"left", sortable:false},
                    { name: 'formName', index: 'stock', width: 80 , align: "center", sortable:false },
                    { name: 'specName', index: 'ship', width: 80, align: "left", sortable:false  },
                    { name: 'factor', index: 'ship', width: 80, align: "center", sortable:false  },
                    { name: 'unitName', index: 'ship', width: 80, align: "center", sortable:false  },
                    { name: 'materialName', index: 'ship', hidden:true,width: 80, align: "center", sortable:false },
                    { name: 'prodCompname', index: 'ship', width: 200, align: "center", sortable:false/*,
						formatter:function(cellvalue, options, row) {
							return "<a href=\"javascript:void(0);\" onclick=\"showCompInfo('"+row.prodCompcode+"', '"+cellvalue+"')\">"+cellvalue+"</a>"
                    	}*/},
                    {name:'drugAttr',index:'drugAttr',sortable: false, width:80, align:"left",sorttype:false,
						formatter : function(val, opts, rowdata) {
							var str = "";
							if (val == 1) {
								str = "国家基本药物";
							} else if (val == 2) {
								str = "非基本药物";
							} else if (val == 3) {
								str = "省增补基本药物";
							} 
							return str;
						}
					},
   		     		{ name: 'goodsCode',index:'n1', width:100,align:"center", sortable:false,hidden:true  },//商品编码
                    { name: 'projName', index: 'sdate', width: 200, align: "center", sortable:false,hidden:true },
                    { name: 'goodsSource', index: 'goodsSource', hidden:true,width: 80, align: "center", sortable:false},
                    { name: 'bakFlag', index: 'goodsSource',hidden:true, width: 80, align: "center", sortable:false},
					{ name: 'catCompName', index: 'catCompName', width: 200, align: "center", sortable:false},
                    { name: 'delCompCode',hidden:true, width: 80, align: "center", sortable:false}
                ],
                rowNum: 20,
                rowList: [10, 20, 50,100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                gridComplete: function(){
                	//序号列宽度自适应（参数为jqgridID）
                    autoRNWidth("gridlist");
                	//数据只有一条时默认选中（参数为jqgridID）
                    selectByOneData("gridlist");
                    var ids = jQuery("#gridlist").jqGrid('getDataIDs');
                    if ('${isSetCatalogPrice}' == 'true'){
	            		for(var i=0;i < ids.length;i++){
	            			var id = ids[i];
	            			var rowdata = $("#gridlist").jqGrid('getRowData', id);
								a = "<a href=\"javascript:$.HN.dialog.opendialog('delId"+id+"','<spring:message code="message.button.SetPeiSongQiYe"/>','${ctx}/purGoodsSelect/examplest1.html?projId="+rowdata.projId+"&goodsCode="+rowdata.goodsCode+"&delId= "+id+"&delTxt="+id+"','800px','400px',operResult)\" style='background:url(${ctx}/lib/img/icon/icon.png) no-repeat -120px -100px' class='opIcon' title='<spring:message code="message.button.SetPeiSongQiYe"/>'></a>";
									c = "<input class=\"\" style=\"width:75px;text-align: right;\" type=\"text\" id=\"proPrice"+id+"\" value=\""+rowdata.proPriceLimit+"\" />";
			            			jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:c,oper:a});
						}
                    } else {
                    	for(var i=0;i < ids.length;i++){
	            			var id = ids[i];
	            			var rowdata = $("#gridlist").jqGrid('getRowData', id);
			            			jQuery("#gridlist").jqGrid('setRowData',ids[i],{proPrice:rowdata.proPriceLimit});
						}
                    }
            	},
                caption: "药品列表",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $("#gridlist").jqGrid("setGridHeight", create());
            $(window).trigger("resize");
        });
        
        
        
        //查询
        function gridSerch(page) {
        	var goodsSource = $.trim($("#goodsSource").val());
        	var projId = $.trim($("#projId").val());
        	var projName = $.trim($("#projName").val());
        	var drugCode = $.trim($("#drugCode").val());
        	var qualityspecName = $.trim($("#qualityspecName").val());
        	var prodCompname = $.trim($("#prodCompname").val());
        	var isLongtimePlan = $.trim($("#isLongtimePlan").val());
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{
    	        		"goodsSource":goodsSource,
    	        		"projId":projId,
    	        		"projName":projName,
    	        		"drugCode":drugCode,
    	        		"qualityspecName":qualityspecName,
    	        		"prodCompname":prodCompname,
    	        		"isLongtimePlan":isLongtimePlan
    	        		
    	        },
    	        page:(page|| 1)
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
      
        
        function batchSaveGoodsSelect(){
            var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
            if (rowIds.length <= 0) {
            	top.$.HN.message.alert("<spring:message code="message.lable.pleaseSelect"/><spring:message code="message.button.JiaRuMuLu"/><spring:message code="message.lable.de"/>药品<spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                return;
            }
            var list = [];
            var goodsInfo = "";
            for (var i=0; i<rowIds.length; i++) {
                rowId = rowIds[i];
                var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
				if(rowdata.delCompName == "" || rowdata.delCompName == null) {
					top.$.HN.message.alert("请设置配送企业！", "<spring:message code="message.HN.alert.type"/>", "warn");
                    return;
				}
               if("${isSetCatalogPrice}" == "true"){
            	   if (rowdata.bakFlag != "true"){
            		   if ($.trim($("#proPrice"+rowId).val()).length < 1){
                          	top.$.HN.message.alert("<spring:message code="message.HN.alert.pleaseTianXieCaiGouJia"/><spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                              return;
                          }else {
                       	   var reg = new RegExp(regexEnum.num);
                       	   if (!reg.test($.trim($("#proPrice"+rowId).val()))){
                       		   top.$.HN.message.alert("\"" + rowdata.qualityspecName + " \"采购价必须为数字<spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                       		  	return;
                       	   }
                       	   if (Number($.trim($("#proPrice"+rowId).val())) <= 0){
                       		top.$.HN.message.alert("\"" + rowdata.qualityspecName + " \"采购价请大于0<spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                            return;
                       	   }
                       	   if(Number($.trim($("#proPrice"+rowId).val())) > Number(rowdata.proPriceLimit) ){
                                 	top.$.HN.message.alert("\"" + rowdata.qualityspecName + " \"采购价不能大于采购限价<spring:message code="message.yanZheng.ganTanHao"/>", "<spring:message code="message.HN.alert.type"/>", "warn");
                                     return;
                                 }
                          }
            	   }
            	   
                	   
               }
               list.push({"goodsCode":rowdata.goodsCode, "proPrice":$.trim($("#proPrice"+rowId).val()),"proLimitPrice":rowdata.proPriceLimit,"drugName":rowdata.qualityspecName});
                if(3>i){
            		goodsInfo+=rowdata.qualityspecName+"<br/>"
                	if(2==i){
                		goodsInfo+="...";
                	}
                }
                
            }
            
            $.HN.message.confirm("确定加入以下药品吗？<br/>"+goodsInfo+"", '', '').on(function (e) {
                if (e) {
                	$("#jrml").attr("disabled", true); 
                    var list = [];
                    for (var i=0; i<rowIds.length; i++) {
                        rowId = rowIds[i];
                        var rowdata = $("#gridlist").jqGrid('getRowData', rowId);
                        list.push({"goodsCode":rowdata.goodsCode, "proPrice":$.trim($("#proPrice"+rowId).val()),"proLimitPrice":rowdata.proPriceLimit,"drugName":rowdata.qualityspecName,"delComp":rowdata.delCompName,"delCompCode":rowdata.delCompCode});
                    }
                    $.post("${ctx}/purHospCatalog/batchSaveGoodsSelect.html", {
                        "array" : json.jsonToString(list)
                    }, function(result) {
                        if (result.success) {
                        	top.$.HN.message.alert("成功添加"+list.length+"个药品", "<spring:message code="message.HN.alert.type"/>", "success");
                        	gridSerch($("#gridlist").getGridParam("page"));
                        	$("#jrml").attr("disabled", false); 
                        } else {
                            top.$.HN.message.alert(result.msg, "<spring:message code="message.HN.alert.type"/>", "error");
                            $("#jrml").attr("disabled", false); 
                        }
                    }, "json")
                }
            });
        }
		 
		 //回调
			  function operResult(val){
	        	var strs = new Array();
	            strs = val.split("|$");
	            	if (val.length>0) {
	            		$("#gridlist").jqGrid('setCell',strs[1],'delCompCode',strs[2]);
	            		$("#gridlist").jqGrid('setCell',strs[1],'delCompName',strs[3]);
					}	
					
	        }
		 
    </script>
</body>
</html>